package com.example.ViewPagerSum.client;

import com.example.ViewPagerSum.util.HttpUtils;
import com.example.ViewPagerSum.util.PhotoId;
import com.example.ViewPagerSum.util.URLTools;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * ============================================================
 * author：     王景
 * <p/>
 * time：       2015/9/22  19:23.
 * <p/>
 * email:     wjontheway@163.com
 * <p/>
 * description：  请求网络数据的工具类
 * <p/>
 * ============================================================
 */
public class HomeClientAPI {
    private HomeClientAPI() {
    }

    /**
     * 接口 2
     * get请求“清纯”的JSON
     *
     * @return
     */
    public static JSONObject getPure() {
        JSONObject ret = null;
        String url = URLTools.BASEURL + "?id=" + PhotoId.PURE + "&rows=100";
        byte[] bytes = HttpUtils.doGet(url);
        if (bytes != null) {
            try {
                String str = new String(bytes, "UTF-8");
                ret = new JSONObject(str);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    /**
     * 接口 5
     * get请求“车模”的JSON
     *
     * @return
     */
    public static JSONObject getCar() {
        JSONObject ret = null;
        String url = URLTools.BASEURL + "?id=" + PhotoId.PORTRAIT + "&rows=60";
        byte[] bytes = HttpUtils.doGet(url);
        if (bytes != null) {
            try {
                String str = new String(bytes, "UTF-8");
                ret = new JSONObject(str);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }


    public static JSONObject getPortrait() {
        JSONObject ret = null;
        String url = URLTools.BASEURL + "/discovery/v1/tabs?device=android";
        byte[] bytes = HttpUtils.doGet(url);
        if (bytes != null) {
            try {
                String str = new String(bytes, "UTF-8");
                ret = new JSONObject(str);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }
}
